package com.lottery.material.service.admin.conf.impl;

import com.lottery.material.dao.conf.AppVersionControlMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.enumeration.conf.AppVersionControlEnum;
import com.lottery.material.pojo.conf.AppVersionControl;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.conf.AppVersionControlCustom;
import com.lottery.material.service.admin.conf.AppVersionControlService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：APP版本控制服务接口实现类
 * Created by 朱正磊 on 2019-05-14 17:33
 */
@Service("adminAppVersionControlService")
public class AppVersionControlServiceImpl implements AppVersionControlService {

    private final AppVersionControlMapper appVersionControlMapper;

    @Autowired
    public AppVersionControlServiceImpl(AppVersionControlMapper appVersionControlMapper) {
        this.appVersionControlMapper = appVersionControlMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param appVersionControl 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(AppVersionControl appVersionControl) {
        appVersionControl.setCreateTime(new Date());//初始化创建时间
        appVersionControl.setState((Integer) CommonEnum.STATE_OFF.getValue());//默认下架
        return appVersionControlMapper.insertSelective(appVersionControl);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return appVersionControlMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public AppVersionControl findById(Integer id) {
        return appVersionControlMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param appVersionControl 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(AppVersionControl appVersionControl) {
        return appVersionControlMapper.updateByPrimaryKeySelective(appVersionControl);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<AppVersionControlCustom> findCustomListPage(Page page) {
        List<AppVersionControlCustom> list = appVersionControlMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public AppVersionControlCustom findCustomById(Integer id) {
        AppVersionControlCustom custom = appVersionControlMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(AppVersionControlCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //APP平台（1：苹果；2：安卓；3：通用）
        custom.setAppPlatformDesc(EnumUtil.getDescriptionByCodeValue(AppVersionControlEnum
                .APP_PLATFORM, custom.getAppPlatform(), AppVersionControlEnum.class));
        //版本控制类型（1：APP；2：银行信息列表；3：省市区信息列表）
        custom.setVersionControlTypeDesc(EnumUtil.getDescriptionByCodeValue(AppVersionControlEnum
                .VERSION_CONTROL_TYPE, custom.getVersionControlType(), AppVersionControlEnum.class));
        //更新类型（1：无须更新；2：选择更新；3：强制更新）
        custom.setUpdateTypeDesc(EnumUtil.getDescriptionByCodeValue(AppVersionControlEnum
                .UPDATE_TYPE, custom.getUpdateType(), AppVersionControlEnum.class));
        //状态 1：上架；2：下架
        custom.setStateDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.STATE, custom.getState(), CommonEnum.class));
    }

    /**
     * description：根据版本号查询数据集合
     * user 朱正磊
     * time 2019-04-28 16:42
     * @param versionNumber 版本号
     * @param id 主键
     * @return 返回数据集合
     */
    @Override
    public List<AppVersionControl> findByVersionNumber(String versionNumber, Integer id) {
        return appVersionControlMapper.findByVersionNumber(versionNumber, id);
    }

}
